# -*- coding: utf-8 -*-

'''eof
name:已结清无逾期发生贷款笔数
code:SettledLoansNum_of_NoOverdue_2D
tableName:
columnName:
groups:个人征信
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:
eof'''


import sys
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None or table == "":
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return [{}]
        elif table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def IC14_SettledLoansNum_of_NoOverdue():
    data = IC_DSJ_2D['data']
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        ## 个人征信信贷交易信息明细（PD01）.基本信息段(PD01A)
        table1 = getTable(data, "PDA", 1)
        if table1 is None:
            return u'缺失值'
        elif table1 == [{}]:
            return u'缺失值'
        else:
            table2 = getTable(table1[0], "PD01",1)
            if table2 is None:
                return u'缺失值'
            elif table2 == [{}]:
                return u'缺失值'
            else:
                factor=[]
                Over_Num=["1","2","3","4","5","6","7"]
                for i in table2:
                    PD01A=getTable(i, "PD01A", 1)
                    PD01B=getTable(i,"PD01B",1)
                    PD01E=getTable(i,"PD01E",0)
                    if PD01E in ([{}],None) or PD01B in ([{}],None) or PD01A in ([{}],None):
                        factor.append(None)
                    elif PD01A[0].get("PD01AD01") not in null_type_list and PD01B[0].get("PD01BD01") not in null_type_list:
                        if PD01A[0].get("PD01AD01")=="D1" and PD01B[0].get("PD01BD01") in ("3","5"):
                            PD01EH=getTable(PD01E[0],"PD01EH",0)
                            for j in PD01EH:
                                Status = []
                                if j.get("PD01ED01") not in null_type_list:
                                    Status.append(j.get("PD01ED01"))
                            Status1=set(Status)
                            #两个列表的交集
                            ret_list = list((set(Status1).union(set(Over_Num))) ^ (set(Status1) ^ set(Over_Num)))
                            if len(ret_list)==0:
                                factor.append(1)
                            else:
                                factor.append(0)
                        elif PD01A[0].get("PD01AD01") in("R1","R4") and PD01B[0].get("PD01BD01")=="3":
                            PD01EH=getTable(PD01E[0],"PD01EH",0)
                            for j in PD01EH:
                                Status = []
                                if j.get("PD01ED01") not in null_type_list:
                                    Status.append(j.get("PD01ED01"))
                            Status1=set(Status)
                            #两个列表的交集
                            ret_list = list((set(Status1).union(set(Over_Num))) ^ (set(Status1) ^ set(Over_Num)))
                            if len(ret_list)==0:
                                factor.append(1)
                            else:
                                factor.append(0)
                        else:
                            factor.append(0)
                    else:
                        factor.append(None)
                factorNotNone = filter(lambda x: x is not None, factor)
                if len(factorNotNone)==0:
                    return u"缺失值"
                else:
                    return  sum(factorNotNone)
    except:
        return u"缺失值"

result=IC14_SettledLoansNum_of_NoOverdue()